//
//  NewFeatureViewController.swift
//  WEIBO_SWIFT
//
//  Created by 岳挺 on 15/6/26.
//  Copyright © 2015年 yueting. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {
    
    let layout = UICollectionViewFlowLayout()
    let imageCount = 4
    
    
    init() {
        super.init(collectionViewLayout :layout)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        layout.itemSize = collectionView!.bounds.size
        layout.minimumLineSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        // Configure the cell
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last!
        if path.item == 3
        {
            (collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell).showClickInBtn()
        }
    }
}



class NewFeatureCell : UICollectionViewCell {
    
    override init(frame : CGRect) {
        super.init(frame : frame)
        addSubview(newFeatureImage)
        addSubview(clickInBtn)
        
        print(imageIndex)
    
        clickInBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: clickInBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: clickInBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(120)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : clickInBtn]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : clickInBtn]))
        
        print(clickInBtn)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var imageIndex : Int = 0 {
        didSet{
            let imageName = "new_feature_\(imageIndex + 1)"
            newFeatureImage.image = UIImage(named: imageName)
            
            clickInBtn.hidden = true
        }
    }
    
    func showClickInBtn (){
        clickInBtn.hidden = false
        clickInBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            self.clickInBtn.transform = CGAffineTransformIdentity
            }, completion: nil)
    }
    
    func beginToExperience (){
        NSNotificationCenter.defaultCenter().postNotificationName(notification, object: true)
    }
    
    //开始体验按钮懒加载
    lazy var clickInBtn : UIButton = {
        var btn = UIButton()
        btn.setTitle("马上体验", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.hidden = true
        btn.addTarget(self, action: "beginToExperience", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    lazy var newFeatureImage : UIImageView = {
        var img = UIImageView()
        return img
        }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        newFeatureImage.frame = self.bounds
    }

}
